package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.scsj.supermarket.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 规格item
 */
public class SpecItemAdapter extends RecyclerView.Adapter<SpecItemAdapter.MyViewHolder> {

    private LayoutInflater minflater;
    private Context mContext;
    protected List<String> mDatas = new ArrayList<>();

    public interface OnItemClickListener {
        void onItemClick(View view, String data, int postion);

    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;

    public SpecItemAdapter(Context context) {
        this.mContext = context;
        minflater = LayoutInflater.from(context);
    }

    public void addAllData(List<String> mDatas) {
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }

    //选中
    public void changeSelected(int id){
//        for(String specBean : mDatas){
//            if(specBean.getID()==id){
//                specBean.setIsSelected(1);
//            }else {
//                specBean.setIsSelected(0);
//            }
//        }
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = minflater.inflate(R.layout.item_spec, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;

    }

    public void add(int pos) {
//        mDatas.add(pos, "insert to");
        notifyItemChanged(pos);
    }

    public void delte(int pos) {
        mDatas.remove(pos);
        notifyItemChanged(pos);
    }


    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, final int i) {
        String specBean = mDatas.get(i);
        myViewHolder.title.setText(specBean);
//        if(specBean.getIsSelected()==1){
//            myViewHolder.title.setTextColor(mContext.getResources().getColor(R.color.tab_selected));
//            myViewHolder.title.setBackgroundResource(R.drawable.shape_spec_selected);
//        }else {
//            myViewHolder.title.setTextColor(mContext.getResources().getColor(R.color.tab_unselected));
//            myViewHolder.title.setBackgroundResource(R.drawable.shape_spec_unselected);
//        }
        setItemEventClick(myViewHolder);
    }

    @Override
    public int getItemCount() {
        if (mDatas != null) {
            return mDatas.size();
        } else {
            return 0;
        }
    }

    protected void setItemEventClick(final MyViewHolder myViewHolder) {
        if (mOnItemClickListener != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int layoutPostion = myViewHolder.getLayoutPosition();
                    String data = mDatas.get(layoutPostion);
                    mOnItemClickListener.onItemClick(view,data, layoutPostion);
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView title;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }

}
